package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * setmeal业务层实现类
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //将数据保存到setmeal表中
        this.save(setmealDto);
        //获取dto中setmealDishes属性
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //循环遍历,给setmeal字段赋值
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //将setmealDishes中的数据保存到setmealDish
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page listSetmeal(Integer page, Integer pageSize, String name) {
        //新建分页page<setmeal>对象
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);//name like %?%
        //执行SQL语句,select * from setmeal where name like %?% limit ?
        this.page(setmealPage,queryWrapper);
        //新建分页page<SetmealDto>对象
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //使用BeanUtils,将setmealPage中除了records的属性复制到setmealDtoPage
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //获取setmealPage中查询结果
        List<Setmeal> setmealList = setmealPage.getRecords();
        //开启setmealList的流
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            //获取setmeal中categoryid的值
            Long id = setmeal.getCategoryId();
            //根据id获取category
            Category category = categoryService.getById(id);
            //获取category的name
            String categoryName = category.getName();
            //创建setmealdto对象
            SetmealDto setmealDto = new SetmealDto();
            //使用BeanUtils,将setmeal的属性复制到setmealDto
            BeanUtils.copyProperties(setmeal, setmealDto);
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        //将setmealDtoList赋给setmealDtoPage中的records中
        setmealDtoPage.setRecords(setmealDtoList);
        return setmealDtoPage;
    }

    @Override
    public void deleteSetmeal(List<Long> ids) throws BusinessException {
        //创建条件构造器,select count(*) from setmeal where status = 1 and id in (?)
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus,"1")
                .in(Setmeal::getId,ids);
        int count = this.count(queryWrapper);
        //说明其中有套餐处于启售状态
        if (count != 0){
            throw new BusinessException("选中的套餐有套餐处于启售状态");
        }
        this.removeByIds(ids);
//        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.in(SetmealDish::getSetmealId,ids);
//        setmealDishService.remove(queryWrapper1);

    }

    @Override
    public void changeStatus(List<Long> ids,Integer status) {
        //update setmeal set  status = ? where id in (?)
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus,status)
                .in(Setmeal::getId,ids);
        this.update(updateWrapper);
    }

    @Override
    public SetmealDto getSetmealById(Long id) {
        //根据id获取setmeal
        Setmeal setmeal = this.getById(id);
        //新建sermealdto对象
        SetmealDto setmealDto = new SetmealDto();
        //使用beanutils.将setmeal的属性赋给setmealdto
        BeanUtils.copyProperties(setmeal,setmealDto);
        //select * from setmealdish where setmealid = ?
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        //将查询到的结果赋给setmealdto中SetmealDishes的属性
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    @Override
    public void saveSetmeal(SetmealDto setmealDto) {
        //将数据根据id更新到setmeal表中
        this.updateById(setmealDto);
        //根据setmealid删除setmealdish表中的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //获取setmealdto中的SetmealDishes的属性
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //循环遍历,将setmeal中的id赋给setmealDish中SetmealId属性
        List<SetmealDish> setmealDishList = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //将上面封装的list集合保存到setmealdish表中
        setmealDishService.saveBatch(setmealDishList);
    }

}
